<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
/**
 * 商品库存表控制器
 */
class StoreController extends CommonController{
	//库存表
	public function index(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
    	//在where条件中增加未删除的判断条件
       	$where['status']=array('eq','1');
       	unset($where['goods_name']);
       	session('excel_where',$where);
		$db=D('StoreView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$field=array('depot_name','supplier_name','code','goods_name','xinghao','guige','unit','price','begin','income','pay','movein','moveout','loss','refund','status','(begin + income - pay + movein - moveout + loss - refund) as final');
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->field($field)->select();
		$this->assign('export_excel',135);//导出报表
		$this->display();
	}
	public function report(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'desc';
		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);
		//加载检索的基础数据
		$this->search();
		//检索条件
        if($_POST['search_where']){
            $this->search_where=$_POST['search_where'];
        	$where=$this->_search();
        }
    	//在where条件中增加未删除的判断条件
       	$where['status']=array('eq','1');
       	unset($where['goods_name']);
		$db=D('StoreView');
		//获取总数据条数
		$count=$db->where($where)->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$field=array('depot_name','supplier_name','sum(begin*price) as Sbegin','sum(pay*price) as Spay','sum(income * price) as Sincome','sum(movein * price) as Smovein','sum(moveout * price) as Smoveout','sum(refund * price) as Srefund');
		$this->list=$db->limit($numPerPage)->Page($pageNum)->order($orders)->where($where)->field($field)->group('supplier_name,depot_name')->select();
		$this->display();
	}
	public function out(){
		$where=session('excel_where');
		$field=array('code','begin','income','pay','movein','moveout','loss','refund','price','goods_name','xinghao','guige','unit','depot_name','supplier_name','(begin + income - pay + movein - moveout + loss - refund) as final');
		$db=D('StoreView');
		$data=$db->where($where)->field($field)->order($orders)->select();
		$row=array('code','depot_name','supplier_name','goods_name','xinghao','guige','unit','price','begin','income','pay','movein','moveout','loss','refund','final');
		$data=array_sort($data,$row);
    	//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
		import("Org.Util.PHPExcel");
		import("Org.Util.PHPExcel.Writer.Excel5");
		import("Org.Util.PHPExcel.IOFactory.php");
		$filename="库存明细";
		$headArr=array('物料编码','仓库名称','供应商名称','物料名称','型号','规格','单位','单价','期初库存','入库数量','支出数量','调入数量','调出数量','损溢数量','退货数量','期末库存');
		$this->getExcel($filename,$headArr,$data);
	}

}